package com.xy.smartsms.db.carrierparam.entity;

import android.content.Context;

import com.xy.smartsms.db.base.MixCursor;
import com.xy.smartsms.db.carrierparam.CarrierDBManager;
import com.xy.smartsms.util.Log;

/**
 * Created by kangwenbo on 2017/3/28.
 */

public class BlackListDb {

    //----- 建表 Start------ //
    public static final String KEY_ID = "_id";
    public static final String KEY_PHONE = "number";
    public static final String KEY_FUNC_BITWISE = "func_bitwise";
    public static final String KEY_DISPLAY_BITWISE = "display_bitwise";
    public static final String KEY_EXTEND = "extend_val";
    public static final String KEY_VERSION = "param_version";
    public static final String KEY_LAST_UPDATE_TIME = "last_update_time";
    public static final String KEY_LAST_UPDATE_STATUS = "last_update_status";

    public static final String NAME = "tb_online_blacklist";

    public static final String DROP_TABLE = " DROP TABLE IF EXISTS " + NAME;
    public static final String CREATE_TABLE = "create table  if not exists "
            + NAME +
            " ("
            + KEY_ID + " INTEGER PRIMARY KEY,"
            + KEY_PHONE + " TEXT,"
            + KEY_FUNC_BITWISE + " LONG,"
            + KEY_DISPLAY_BITWISE + " LONG,"
            + KEY_EXTEND + " TEXT,"
            + KEY_VERSION + " VARCHAR,"
            + KEY_LAST_UPDATE_TIME + " LONG,"
            + KEY_LAST_UPDATE_STATUS + " INTEGER"
            + ")";

    //----- 建表 end------ //

    private final String TAG = "BlackListDb";
    private static BlackListDb sBlackListDb = null;
    private Context mContext;

    private BlackListDb (Context context) {
        mContext = context;
    }

    public static BlackListDb getInstance() {
        return sBlackListDb;
    }

    public static BlackListDb getInstance(Context context) {
        synchronized (BlackListDb.class) {
            if (sBlackListDb == null) {
                sBlackListDb = new BlackListDb(context);
            }
        }
        return sBlackListDb;
    }

    public BlackListItem queryByPhone(String phone) {
        BlackListItem item = null;
        MixCursor cursor = null;
        try {
            cursor = CarrierDBManager.query(mContext, NAME,
                    null,
                    KEY_PHONE + "=?",
                    new String[]{phone}
            );
            if (cursor != null && cursor.moveToFirst()) {
                item = cursorToBlackItem(cursor);
            } else {
                item = new BlackListItem(phone);
            }
        } catch (Throwable t) {
            Log.e(TAG, "queryByPhone", t);
        } finally {
            MixCursor.closeCursor(cursor, true);
        }
        return item;
    }

    /**
     * cursor 转blackitem, 只转一次，若有多次操作，请另外处理
     *
     * @param cursor
     * @return
     */
    private BlackListItem cursorToBlackItem(MixCursor cursor) {
        BlackListItem item = new BlackListItem();
        item.setId(cursor.getInt(cursor.getColumnIndex(KEY_ID)));
        item.setPhone(cursor.getString(cursor.getColumnIndex(KEY_PHONE)));
        item.setFuncBitwise(cursor.getLong(cursor.getColumnIndex(KEY_FUNC_BITWISE)));
        item.setDisplayBitwise(cursor.getLong(cursor.getColumnIndex(KEY_DISPLAY_BITWISE)));
        item.setExtend(cursor.getString(cursor.getColumnIndex(KEY_EXTEND)));
        item.setVersion(cursor.getString(cursor.getColumnIndex(KEY_VERSION)));
        item.setLastUpdateTime(cursor.getLong(cursor.getColumnIndex(KEY_LAST_UPDATE_TIME)));
        item.setLastUpdateStatus(cursor.getInt(cursor.getColumnIndex(KEY_LAST_UPDATE_STATUS)));
        return item;
    }

    public void insertOrSkip(BlackListItem item) {
        MixCursor cursor = null;
        try {
            cursor = CarrierDBManager.query(mContext, NAME,
                    null,
                    KEY_PHONE + "=?",
                    new String[]{item.getPhone()}
            );
            if (cursor == null || (cursor != null && cursor.moveToFirst())) {
                return;
            }
        } catch (Throwable t) {
            Log.e(TAG, "insertOrSkip", t);
        } finally {
            MixCursor.closeCursor(cursor, true);
        }

        try {
            CarrierDBManager.insert(mContext, NAME, item.getContentValues());
        } catch (Throwable t) {
            Log.e(TAG, "insertOrSkip", t);
        }
    }

    public void updateOrInsert(BlackListItem item) {
        try {
            int row = CarrierDBManager.update(mContext, NAME, item.getContentValues(), KEY_PHONE + "=?",
                    new String[]{item.getPhone()});
            if (row <= 0) {
                CarrierDBManager.insert(mContext, NAME, item.getContentValues());
            }
        } catch (Throwable t) {
            Log.e(TAG, "updateOrInsert", t);
        }
    }

    public void delete(BlackListItem item) {
        try {
            CarrierDBManager.delete(mContext, NAME, KEY_PHONE + "=?", new String[]{item.getPhone()});
        } catch (Throwable t) {
            Log.e(TAG, "delete", t);
        }
    }
}
